//
//  shitangTableViewController.m
//  shuilijuOASystem
//
//  Created by michael on 16/8/15.
//  Copyright © 2016年 czd. All rights reserved.
//

#import "shitangTableViewController.h"
#import "PickerChoiceView.h"
#import "selectview.h"
#import "GDataDefines.h"

@interface shitangTableViewController ()
@property (nonatomic,copy)NSString *danweitring;
@property (nonatomic,copy)NSString *personstring;
@end

@implementation shitangTableViewController

- (void)viewDidLoad {
    [super viewDidLoad];
   self.title = @"申请食堂";
       NSDate *currentDate = [NSDate date];//获取当前时间，日期
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:@"YYYY-MM-dd"];
_date = [dateFormatter stringFromDate:currentDate];
_datestring = _date;
   
    //点击手势
 UITapGestureRecognizer * tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(keybordDown)];
    [self.view addGestureRecognizer:tap];
[self  loaddata];
    UIBarButtonItem * rightIem = [[UIBarButtonItem alloc] initWithTitle:@"发布" style:UIBarButtonItemStylePlain target:self action:@selector(searchAction)];
        self.navigationItem.rightBarButtonItem = rightIem;
}
- (void)loaddata{
NSString * userId = [[NSUserDefaults standardUserDefaults]objectForKey:@"userId"];
    NSArray * array = @[@{@"sName":userId}];
           [HttpTools requestWithURL:@"exchlogistics"
     arrays:array method:@"getCanteenInfo" success:^(id JSON) {
     _dic = JSON;
     
     _danweitring = [NSString stringWithFormat:@"%@",_dic[@"UserDepart"]];
     _personstring = [NSString stringWithFormat:@"%@",_dic[@"Author"]];
//     _array = [string componentsSeparatedByString:@","];
     
           [self.tableView reloadData];
          } failure:^(NSError *error) {
     [self.tableView headerEndRefreshing];
        
    }];
}
- (void)searchAction{
if (_danweiField.text ==nil) {
    [self.view makeToast:@"请填写处室(单位)"];

}
if (_personField0.text ==nil) {
    [self.view makeToast:@"请填写经办人"];

}
if (_datestring ==nil) {
    [self.view makeToast:@"请填写接待日期"];

}
if (_workField.text ==nil) {
    [self.view makeToast:@"请填写接待单位"];

}
if (_personField.text ==nil) {
    [self.view makeToast:@"请填写接待人数"];

}
if (_personField1.text ==nil) {
    [self.view makeToast:@"请填写陪同人数"];

}
if (_datestring1 ==nil) {
    [self.view makeToast:@"请填写用餐时间"];

}
if ([_selectview.orderString isEqualToString:@"1"]) {
    _select1 = @"午餐";
}else{
_select1 = @"晚餐";

}
if ([_selectview1.orderString isEqualToString:@"1"]) {
    _select2 = @"自助餐";
}
else{
_select2 = @"餐桌";

}
if ([_selectview2.orderString isEqualToString:@"3"]) {
    _select3 = @"60";
}else if ([_selectview2 .orderString isEqualToString:@"2"]){

  _select3 = @"40";
}
else{

_select3 = @"20";
}
if ([_selectview3.orderString isEqualToString:@"1"]) {
    _select4 = @"A";
}else if ([_selectview3.orderString isEqualToString:@"2"]){

_select4 = @"B";

}else if([_selectview3.orderString isEqualToString:@"3"]){

_select4 = @"C";
}else if([_selectview3.orderString isEqualToString:@"4"]){

_select4 = @"D";
}else {

_select4 = @"E";
}
if (_danweiField.text!=nil&&_personField0.text!=nil&&_datestring!=nil&&_workField.text!=nil&&_personField.text!=nil&&_personField1.text!=nil&&_datestring1!=nil) {
    NSString * userId = [[NSUserDefaults standardUserDefaults]objectForKey:@"userId"];

     NSString *json =[NSString stringWithFormat: @"{'csdw':'%@','jbr':'%@','jdTime':'%@','jddw':'%@','jdperson':'%@','ptperson':'%@','eatingtime':'%@','eating':'%@','eatingfs':'%@','bz':'%@','jds':'%@'}",_danweiField.text,_personField0.text,_datestring,_workField.text,_personField.text,_personField1.text,_datestring1,_select1,_select2,_select3,_select4];
    NSArray * array = @[@{@"json":json},@{@"userId":userId}];
           [HttpTools postWithURL:exchlogistics
     arrays:array method:@"saveCanteen" success:^(id JSON) {
     NSData * data = [JSON dataUsingEncoding:NSUTF8StringEncoding];
       GDataXMLDocument *doc = [[GDataXMLDocument alloc] initWithData: data options:0 error:nil];
         GDataXMLElement *rootElement = [doc rootElement];
    NSArray *users = [rootElement children];
     NSArray *infoData= [rootElement elementsForName:@"info"];
    for (int i = 0;i<[infoData count];i++)
    {
        GDataXMLElement *infoElement = [infoData objectAtIndex:i];
        self.infoString = [infoElement stringValue];
    }


     if ([_infoString isEqualToString:@"11"]) {
     [self.view makeToast:@"发送成功!"];
     [self .navigationController popViewControllerAnimated:YES];
}else{
 [self.view makeToast:@"发送失败!"];
      }
      
    } failure:^(NSError *error) {
        
    }];

}


}








- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
}
- (void)keybordDown {
    [self.view endEditing:YES];
    }

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
 if (indexPath.row==0) {
 
  static NSString *cellid0 = @"cell0";
    UITableViewCell *cell = (UITableViewCell *)[tableView dequeueReusableCellWithIdentifier:cellid0];
    if (!cell) {
        cell=[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellid0];      UILabel *label = [[UILabel alloc]initWithFrame:CGRectMake(10, 0, 100, cell.contentView.height)];
    label.text = @"处室(单位):";
     label.font = [UIFont systemFontOfSize:14];
    [cell.contentView addSubview:label];
  _danweiField = [[UITextView alloc]init];
      _danweiField.frame = CGRectMake(label.right, label.top+10, KScreenWidth-120, cell.height-10);
     // UIColor *myColor = [UIColor colorWithWhite:0.5 alpha:0.2];
    //  _nameField.backgroundColor = myColor;
 [_danweiField setFont:[UIFont systemFontOfSize:13]];
 _danweiField.textAlignment = NSTextAlignmentLeft;
      _danweiField.delegate = self;
       [cell.contentView addSubview:_danweiField];

    
    
    }
                   _danweiField.text = _danweitring;

    return cell;
 
 }
else if (indexPath.row==1) {
 
static NSString *cellid1 = @"cell1";
    UITableViewCell *cell = (UITableViewCell *)[tableView dequeueReusableCellWithIdentifier:cellid1];
    if (!cell) {
        cell=[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellid1];
      UILabel *label = [[UILabel alloc]initWithFrame:CGRectMake(10, 0, 100, cell.contentView.height)];
    label.text = @"经办人:";
     label.font = [UIFont systemFontOfSize:14];
    [cell.contentView addSubview:label];
 _personField0 = [[UITextView alloc]init];
      _personField0.frame = CGRectMake(label.right, label.top+10, KScreenWidth-120, cell.height-10);
     // UIColor *myColor = [UIColor colorWithWhite:0.5 alpha:0.2];
    //  _nameField.backgroundColor = myColor;
 [_personField0 setFont:[UIFont systemFontOfSize:13]];
 _personField0.textAlignment = NSTextAlignmentLeft;
      _personField0.delegate = self;
       [cell.contentView addSubview:_personField0];
    }
                   _personField0.text = _personstring;

  return cell;

 }
 
else if (indexPath.row==2) {
 
static NSString *cellid2 = @"cell2";
    UITableViewCell *cell = (UITableViewCell *)[tableView dequeueReusableCellWithIdentifier:cellid2];
    if (!cell) {
        cell=[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellid2];      UILabel *label = [[UILabel alloc]initWithFrame:CGRectMake(10, 0, 100, cell.contentView.height)];
    label.text = @"接待日期 :";
     label.font = [UIFont systemFontOfSize:14];
    [cell.contentView addSubview:label];
   _databutton = [UIButton buttonWithType:UIButtonTypeCustom];
      _databutton.frame = CGRectMake(label.right, label.top, 100, label.height);
      
    [_databutton setTitle:_date forState:UIControlStateNormal];
      
          _databutton.titleLabel.font = [UIFont systemFontOfSize: 13.0];
     [_databutton setTitleColor:[UIColor blackColor]forState:UIControlStateNormal];
     [_databutton addTarget:self action:@selector(dataaction) forControlEvents:UIControlEventTouchUpInside];
     _databutton.layer.borderColor = [[UIColor blackColor] CGColor];
       [cell.contentView addSubview:_databutton];
 
  }
  return cell;

 }
else if (indexPath.row==3) {
 
     static NSString *cellid3 = @"cell3";
    UITableViewCell *cell = (UITableViewCell *)[tableView dequeueReusableCellWithIdentifier:cellid3];
    if (!cell) {
        cell=[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellid3];

      UILabel *label = [[UILabel alloc]initWithFrame:CGRectMake(10, 0, 100, cell.contentView.height)];
    label.text = @"接待单位 :";
     label.font = [UIFont systemFontOfSize:14];
    [cell.contentView addSubview:label];
   _workField = [[UITextView alloc]init];
           _workField.layer.borderColor = [[UIColor blackColor] CGColor];
      _workField.frame = CGRectMake(label.right, label.top+10, KScreenWidth-120, cell.height-10);
      UIColor *myColor = [UIColor colorWithWhite:0.5 alpha:0.2];
      _workField.backgroundColor = myColor;
 [_workField setFont:[UIFont systemFontOfSize:12]];
      _workField.delegate = self;
       [cell.contentView addSubview:_workField];}
             return cell;

 }
else if (indexPath.row==4) {
 
 static NSString *cellid4 = @"cell4";
    UITableViewCell *cell = (UITableViewCell *)[tableView dequeueReusableCellWithIdentifier:cellid4];
    if (!cell) {
        cell=[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellid4];      UILabel *label = [[UILabel alloc]initWithFrame:CGRectMake(10, 0, 100, cell.contentView.height)];
    label.text = @"接待人数 :";
     label.font = [UIFont systemFontOfSize:14];
    [cell.contentView addSubview:label];
   _personField = [[UITextView alloc]init];
           _workField.layer.borderColor = [[UIColor blackColor] CGColor];
      _personField.frame = CGRectMake(label.right, label.top+10, KScreenWidth-120, cell.height-10);
      UIColor *myColor = [UIColor colorWithWhite:0.5 alpha:0.2];
      _personField.backgroundColor = myColor;
 [_personField setFont:[UIFont systemFontOfSize:12]];
      _personField.delegate = self;
       [cell.contentView addSubview:_personField];}
             return cell;

 }

else if (indexPath.row==5) {
 
static NSString *cellid5 = @"cell5";
    UITableViewCell *cell = (UITableViewCell *)[tableView dequeueReusableCellWithIdentifier:cellid5];
    if (!cell) {
        cell=[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellid5];      UILabel *label = [[UILabel alloc]initWithFrame:CGRectMake(10, 0, 100, cell.contentView.height)];
    label.text = @"陪同人数 :";
     label.font = [UIFont systemFontOfSize:14];
    [cell.contentView addSubview:label];
   _personField1 = [[UITextView alloc]init];
           _personField1.layer.borderColor = [[UIColor blackColor] CGColor];
      _personField1.frame = CGRectMake(label.right, label.top+10, KScreenWidth-120, cell.height-10);
      UIColor *myColor = [UIColor colorWithWhite:0.5 alpha:0.2];
      _personField1.backgroundColor = myColor;
 [_personField1 setFont:[UIFont systemFontOfSize:12]];
      _personField1.delegate = self;
       [cell.contentView addSubview:_personField1];}
             return cell;

 }
else if (indexPath.row==6) {
 
static NSString *cellid6 = @"cell6";
    UITableViewCell *cell = (UITableViewCell *)[tableView dequeueReusableCellWithIdentifier:cellid6];
    if (!cell) {
        cell=[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellid6];
      UILabel *label = [[UILabel alloc]initWithFrame:CGRectMake(10, 0, 100, cell.contentView.height)];
    label.text = @"用餐时间 :";
     label.font = [UIFont systemFontOfSize:14];
    [cell.contentView addSubview:label];
   _databutton1 = [UIButton buttonWithType:UIButtonTypeCustom];
      _databutton1.frame = CGRectMake(label.right, label.top, 100, label.height);
      
    [_databutton1 setTitle:@"选择日期" forState:UIControlStateNormal];
      
          _databutton1.titleLabel.font = [UIFont systemFontOfSize: 13.0];
     [_databutton1 setTitleColor:[UIColor blackColor]forState:UIControlStateNormal];
     [_databutton1 addTarget:self action:@selector(dataaction1) forControlEvents:UIControlEventTouchUpInside];
     _databutton1.layer.borderColor = [[UIColor blackColor] CGColor];
       [cell.contentView addSubview:_databutton1];
      
      _selectview = [selectdownbox checkBoxFootView];
      _selectview.frame = CGRectMake(_databutton1.right, label.top, 100, label.height);
       [cell.contentView addSubview:_selectview];
 }
      
             return cell;

 }
else if (indexPath.row==7) {
 
static NSString *cellid7 = @"cell7";
    UITableViewCell *cell = (UITableViewCell *)[tableView dequeueReusableCellWithIdentifier:cellid7];
    if (!cell) {
        cell=[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellid7];      UILabel *label = [[UILabel alloc]initWithFrame:CGRectMake(10, 0, 100, cell.contentView.height)];
    label.text = @"用餐方式 :";
     label.font = [UIFont systemFontOfSize:14];
    [cell.contentView addSubview:label];
    
      _selectview1 = [eatingbox checkBoxFootView];
      _selectview1.frame = CGRectMake(label.right, label.top, 150, label.height);
       [cell.contentView addSubview:_selectview1];}
          return cell;

 }
else if (indexPath.row==8) {
 
static NSString *cellid8 = @"cell8";
    UITableViewCell *cell = (UITableViewCell *)[tableView dequeueReusableCellWithIdentifier:cellid8];
    if (!cell) {
        cell=[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellid8];
      UILabel *label = [[UILabel alloc]initWithFrame:CGRectMake(10, 0, 100, cell.contentView.height)];
    label.text = @"标准(元/人) :";
     label.font = [UIFont systemFontOfSize:14];
    [cell.contentView addSubview:label];
    
      _selectview2 = [selectupboxView checkBoxFootView];
      _selectview2.frame = CGRectMake(label.right, label.top, 150, label.height);
       [cell.contentView addSubview:_selectview2];}
          return cell;

 }

else {
static NSString *cellid9 = @"cell9";
    UITableViewCell *cell = (UITableViewCell *)[tableView dequeueReusableCellWithIdentifier:cellid9];
    if (!cell) {
        cell=[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellid9];        UILabel *label = [[UILabel alloc]initWithFrame:CGRectMake(10, 0, 100, cell.contentView.height)];
    label.text = @"接待室 :";
     label.font = [UIFont systemFontOfSize:14];
    [cell.contentView addSubview:label];
    
      _selectview3 = [jiedaiselectbox checkBoxFootView];
      _selectview3.frame = CGRectMake(label.right, label.top, 150, label.height);
       [cell.contentView addSubview:_selectview3];}
          return cell;
}




}
//日期选择
- (void)dataaction{
 _selectDatePicker = [[MHDatePicker alloc] init];
    _selectDatePicker.isBeforeTime = YES;
    _selectDatePicker.datePickerMode = UIDatePickerModeDate;
    
    __weak typeof(self) weakSelf = self;
    [_selectDatePicker didFinishSelectedDate:^(NSDate *selectedDate) {
      NSString *string = [weakSelf dateStringWithDate:selectedDate DateFormat:@"yyyy-MM-dd"];
      _datestring = string;
      [weakSelf.databutton setTitle:string forState:UIControlStateNormal];
    }];

}

- (void)textViewDidChange:(UITextView *)textView
{
if(textView ==_danweiField){
_danweitring = _danweiField.text;
}
if(textView ==_personField0){
_personstring = _personField0.text;
}
}
- (void)dataaction1{
 _selectDatePicker1 = [[MHDatePicker alloc] init];
    _selectDatePicker1.isBeforeTime = YES;
    _selectDatePicker1.datePickerMode = UIDatePickerModeDate;
    
    __weak typeof(self) weakSelf = self;
    [_selectDatePicker1 didFinishSelectedDate:^(NSDate *selectedDate) {
      NSString *string = [weakSelf dateStringWithDate:selectedDate DateFormat:@"yyyy-MM-dd"];
      _datestring1 = string;
      [weakSelf.databutton1 setTitle:string forState:UIControlStateNormal];
    }];

}
- (NSString *)dateStringWithDate:(NSDate *)date DateFormat:(NSString *)dateFormat
{
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
    [dateFormatter setDateFormat:dateFormat];
    [dateFormatter setLocale:[[NSLocale alloc] initWithLocaleIdentifier:@"zh_CN"]];
    NSString *str = [dateFormatter stringFromDate:date];
    return str ? str : @"";
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return 10;
}

@end
